﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Meishi
{
    /// <summary>
    /// CreatPollPage.xaml 的交互逻辑
    /// </summary>
    public partial class CreatePollPage : Page
    {
        public CreatePollPage()
        {
            InitializeComponent();

            vm = this.DataContext as ViewModel.PollViewModel;


            GridContent.Children.Clear();
            GridContent.Children.Add(new View.Meishi.Partial.PollOne());
        }


        ViewModel.PollViewModel vm;
       

        private void btn_Last_Click(object sender, RoutedEventArgs e)
        {
            stp_Poll.Prev();
            GridContent.Children.Clear();
            if(stp_Poll.StepIndex == 0)
            {
                GridContent.Children.Add(new View.Meishi.Partial.PollOne());
                btn_Last.Visibility = Visibility.Collapsed;
            }
            if (stp_Poll.StepIndex == 1)
            {
                GridContent.Children.Add(new View.Meishi.Partial.PollTwo());
                btn_Finish.Visibility = Visibility.Collapsed;
                btn_Next.Visibility = Visibility.Visible;
            }
             
            

           
        }

        Model.DBContext dbContext = new Model.DBContext();
        private void btn_Finish_Click(object sender, RoutedEventArgs e)
        {
            //验证
            ///var n =  dbContext.DB.Queryable<Model.Poll>().Where(p => (p.StartDate <= vm.CurrentPoll.StartDate && p.EndDate > vm.CurrentPoll.StartDate) || (vm.CurrentPoll.EndDate >= p.EndDate && vm.CurrentPoll.EndDate < p.StartDate)).Count();
            //if (n > 0)
            //{
            //    Growl.Warning("已存在同日期都投票，当日仅能发布一次投票！");
            //    return;
            //}

            //设置现有投票的发布为否
            dbContext.DB.Updateable<Model.Poll>()
                .SetColumns(p => new Model.Poll { IsPublic = false })
                .Where(p => p.IsPublic == true)
                .ExecuteCommand();

                

            vm.CurrentPoll.IsPublic = true;


            dbContext.DB.Insertable<Model.Poll>(vm.CurrentPoll).ExecuteCommand();

            //保存参选菜品
            foreach (var item in vm.ZhucaiMenu)
            {
                dbContext.DB.Insertable<Model.PollItem>(item).ExecuteCommand();
            }

            foreach (var item in vm.ZhushiMenu )
            {
                dbContext.DB.Insertable<Model.PollItem>(item).ExecuteCommand();
            }

            foreach (var item in vm.TangMenu)
            {
                 
                dbContext.DB.Insertable<Model.PollItem>(item).ExecuteCommand();
            }
             

            btn_Finish.IsEnabled = false;
            btn_Last.Visibility = Visibility.Hidden;
            //PublishFinish
            GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<string>("创建完成", "PublishFinish");

            vm.CurrentPoll = new Model.Poll() { StartDate=DateTime.Now,EndDate=DateTime.Now.AddDays(1)};
            vm.ZhushiMenu .Clear();
            vm.ZhucaiMenu.Clear();
            vm.TangMenu.Clear();
        }

        private void btn_Next_Click(object sender, RoutedEventArgs e)
        {
           
            if (stp_Poll.StepIndex == 0)
            {
                //校验
                if(string.IsNullOrEmpty( vm.CurrentPoll.Name))
                {
                    Growl.WarningGlobal("请输入活动标题！");
                    return;
                }
                if (string.IsNullOrEmpty(vm.CurrentPoll.Remark))
                {
                    Growl.WarningGlobal("请输入活动内容！");
                    return;
                }



                if (vm.CurrentPoll.EndDate < DateTime.Now.Date)
                {
                    Growl.WarningGlobal("结束日期不能早于当前日期！");
                    return;
                }
                if (vm.CurrentPoll.StartDate>vm.CurrentPoll.EndDate )
                {
                    Growl.WarningGlobal("结束日期不能早于开始日期！");
                    return;
                }
                GridContent.Children.Clear();
                GridContent.Children.Add(new View.Meishi.Partial.PollTwo());
                btn_Last.Visibility = Visibility.Visible;

                vm.CurrentPoll.Id = dbContext.DB.Queryable<Model.Poll>().Max(p => p.Id) + 1 ;
            }
            else if (stp_Poll.StepIndex == 1)
            {
                if (vm.ZhucaiMenu.Count <=0)
                {
                    Growl.WarningGlobal("请添加主菜类参选菜品！");
                    return;
                }

                if (vm.ZhushiMenu.Count <= 0)
                {
                    Growl.WarningGlobal("请添加主食类参选菜品！");
                    return;
                }

                if (vm.TangMenu.Count <= 0)
                {
                    Growl.WarningGlobal("请添加汤类参选菜品！");
                    return;
                }
                GridContent.Children.Clear();
                GridContent.Children.Add(new View.Meishi.Partial.PollThree());
                btn_Finish.Visibility = Visibility.Visible;
                btn_Next.Visibility = Visibility.Collapsed;
            }

            stp_Poll.Next();
        }
    }
}
